﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;

namespace Thor.Common
{
    public class ProviderInfo : IProviderInfo, INotifyPropertyChanged, IEditableObject
    {
        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Constants

        private const string NAME = "Name";
        private const string PATH = "Path";
        private const string ID = "Id";

        #endregion

        #region Fields

        private string _name = string.Empty;
        private string _path = string.Empty;
        private Guid _id = Guid.Empty;
        private ProviderInfo _cache = null;

        #endregion

        #region Properties

        [Key]
        public Guid Id 
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged(ID);
                }
            }
        }

        [Required]
        public string Name 
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged(NAME);
                }
            }
        }

        public string Path 
        {
            get { return _path; }
            set
            {
                if (_path != value)
                {
                    _path = value;
                    OnPropertyChanged(PATH);
                }
            }
        }

        public byte[] Bytes { get; set; }

        #endregion

        #region Methods

        public void BeginEdit()
        {
            _cache = new ProviderInfo();
            _cache.Path = this.Path;
            _cache.Name = Name;
            _cache.Id = this.Id;
        }

        public void CancelEdit()
        {
            this.Name = _cache.Name;
            this.Path = _cache.Path;
            this.Id = _cache.Id;
        }

        public void EndEdit()
        {
            _cache = null;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
